<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:shop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="true"/>
      <el-table-column label="用户user_id" align="center" prop="userId" />
      <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column label="appid" align="center" prop="appid" />
      <el-table-column label="用户id" align="center" prop="uid" />
      <el-table-column label="入驻类型" align="center" prop="enterType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.usr_enter_type" :value="scope.row.enterType"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="店铺logo" align="center" prop="shopLogo" />
      <el-table-column label="店铺背景" align="center" prop="shopBg" />
      <el-table-column label="店铺环境" align="center" prop="imgs" />
      <el-table-column label="店铺标签" align="center" prop="label" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="电话" align="center" prop="tel" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="配送费" align="center" prop="sendFee" />
      <el-table-column label="配送时间" align="center" prop="sendTime" />
      <el-table-column label="店铺简介" align="center" prop="intro" />
      <el-table-column label="公告" align="center" prop="notice" />
      <el-table-column label="客服id" align="center" prop="kfs" />
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="评价数" align="center" prop="commentNum" />
      <el-table-column label="月售" align="center" prop="monthSale" />
      <el-table-column label="总销" align="center" prop="totalSale" />
      <el-table-column label="权重" align="center" prop="weight" />
      <el-table-column label="抽成比例" align="center" prop="ratio" />
      <el-table-column label="是否下架" align="center" prop="isPutaway">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.usr_logic" :value="scope.row.isPutaway"/>
        </template>
      </el-table-column>
      <el-table-column label="下架原因" align="center" prop="putawayReason" />
      <el-table-column label="下架处理用户id" align="center" prop="putawayUid" />
      <el-table-column label="下架时间" align="center" prop="putawayTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.putawayTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户user_id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户user_id" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="appid" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入appid" />
        </el-form-item>
        <el-form-item label="用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="入驻类型" prop="enterType">
          <el-select v-model="form.enterType" placeholder="请选择入驻类型">
            <el-option
              v-for="dict in dict.type.usr_enter_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺logo" prop="shopLogo">
          <el-input v-model="form.shopLogo" placeholder="请输入店铺logo" />
        </el-form-item>
        <el-form-item label="店铺背景" prop="shopBg">
          <el-input v-model="form.shopBg" placeholder="请输入店铺背景" />
        </el-form-item>
        <el-form-item label="店铺环境" prop="imgs">
          <el-input v-model="form.imgs" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="店铺标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入店铺标签" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="配送费" prop="sendFee">
          <el-input v-model="form.sendFee" placeholder="请输入配送费" />
        </el-form-item>
        <el-form-item label="配送时间" prop="sendTime">
          <el-input v-model="form.sendTime" placeholder="请输入配送时间" />
        </el-form-item>
        <el-form-item label="店铺简介" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="公告" prop="notice">
          <el-input v-model="form.notice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="客服id" prop="kfs">
          <el-input v-model="form.kfs" placeholder="请输入客服id" />
        </el-form-item>
        <el-form-item label="评分" prop="score">
          <el-input v-model="form.score" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="评价数" prop="commentNum">
          <el-input v-model="form.commentNum" placeholder="请输入评价数" />
        </el-form-item>
        <el-form-item label="月售" prop="monthSale">
          <el-input v-model="form.monthSale" placeholder="请输入月售" />
        </el-form-item>
        <el-form-item label="总销" prop="totalSale">
          <el-input v-model="form.totalSale" placeholder="请输入总销" />
        </el-form-item>
        <el-form-item label="权重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入权重" />
        </el-form-item>
        <el-form-item label="抽成比例" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入抽成比例" />
        </el-form-item>
        <el-form-item label="是否下架" prop="isPutaway">
          <el-select v-model="form.isPutaway" placeholder="请选择是否下架">
            <el-option
              v-for="dict in dict.type.usr_logic"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下架原因" prop="putawayReason">
          <el-input v-model="form.putawayReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下架处理用户id" prop="putawayUid">
          <el-input v-model="form.putawayUid" placeholder="请输入下架处理用户id" />
        </el-form-item>
        <el-form-item label="下架时间" prop="putawayTime">
          <el-date-picker clearable
            v-model="form.putawayTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择下架时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop } from "@/api/mall/shop";

export default {
  name: "Shop",
  dicts: ['usr_enter_type', 'usr_logic'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺信息表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        deptId: undefined,
        appid: undefined,
        uid: undefined,
        enterType: undefined,
        shopName: undefined,
        shopLogo: undefined,
        shopBg: undefined,
        imgs: undefined,
        label: undefined,
        address: undefined,
        tel: undefined,
        longitude: undefined,
        latitude: undefined,
        sendFee: undefined,
        sendTime: undefined,
        intro: undefined,
        notice: undefined,
        kfs: undefined,
        score: undefined,
        commentNum: undefined,
        monthSale: undefined,
        totalSale: undefined,
        weight: undefined,
        ratio: undefined,
        isPutaway: undefined,
        putawayReason: undefined,
        putawayUid: undefined,
        putawayTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店铺信息列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        deptId: undefined,
        appid: undefined,
        uid: undefined,
        enterType: undefined,
        shopName: undefined,
        shopLogo: undefined,
        shopBg: undefined,
        imgs: undefined,
        label: undefined,
        address: undefined,
        tel: undefined,
        longitude: undefined,
        latitude: undefined,
        sendFee: undefined,
        sendTime: undefined,
        intro: undefined,
        notice: undefined,
        kfs: undefined,
        score: undefined,
        commentNum: undefined,
        monthSale: undefined,
        totalSale: undefined,
        weight: undefined,
        ratio: undefined,
        isPutaway: undefined,
        putawayReason: undefined,
        putawayUid: undefined,
        putawayTime: undefined,
        createTime: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getShop(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除店铺信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delShop(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mall/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
